package com.ppg.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Properties;

@CrossOrigin
@RestController
@RequestMapping("/interview")
public class TestController {

    @NacosValue(value = "${nacos.aa}", autoRefreshed = true)
    private String aa;

    @RequestMapping(value = "/print", method = RequestMethod.POST)
    public void print(HttpServletRequest request) throws Exception {

        System.out.println(aa);
//        System.out.println(jsonString);

//        System.out.println(id);
//
//        System.out.println(JSON.toJSONString(userVO));

        ServletInputStream inputStream = request.getInputStream();

        System.out.println(IOUtils.toString(inputStream, "utf-8"));

        Map<String, String[]> parameterMap = request.getParameterMap();

        System.out.println(JSON.toJSONString(parameterMap));

        String id1 = request.getParameter("id");
        System.out.println(id1);

        String id2 = request.getParameter("id");
        System.out.println(id2);

//        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
//
//        ServletFileUpload sfu = new ServletFileUpload(fileItemFactory);
//
//        try {
//            List<FileItem> fileItems = sfu.parseRequest(request);
//
//            System.out.println(fileItems);
//        } catch (FileUploadException e) {
//            e.printStackTrace();
//        }
    }

    @PostMapping("/test")
    public void test() {
        try {
            String serverAddr = "172.16.248.213:8848";
            String dataId = "com.jdd.yunc.user.config";
            String group = "yunc:user";
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
            properties.put(PropertyKeyConst.NAMESPACE, "6a13990e-fccd-4de6-95d5-01c1f0b5aa0a");
            properties.put(PropertyKeyConst.ENDPOINT, "test");

            ConfigService configService = NacosFactory.createConfigService(properties);
            String content = configService.getConfig(dataId, group, 5000);
            System.out.println(content);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
